package com.wodwl.controller;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wodwl.dao.AppDao;
import com.wodwl.dao.AppDao.Mode;
import com.wodwl.dao.UserDao;
import com.wodwl.entity.App;
import com.wodwl.entity.User;


@Controller
public class AppController {
	private String app_add_view="/app/app_add";
	private String app_add_success_view="/app/app_list";
	private String app_list_view="/app/app_list";
	private String app_show_view="/app/app_show";
	private String app_editor_view="/app/app_editor";

	
	@RequestMapping("/app/app_editor1.do")
	 public ModelAndView editorInfo1(HttpServletRequest request,HttpServletResponse response) {

		Map map=new Hashtable();
		AppDao dao2=new AppDao();
		App app=dao2.getAppByAid(Integer.parseInt(request.getParameter("aid")));
		map.put("app", app);
		return new ModelAndView(getApp_editor_view(),map);
	}
	
	
	@RequestMapping("/app/app_editor2.do")
	 public ModelAndView editorInfo2(HttpServletRequest request,HttpServletResponse response) {
		
		App app=new App();
		app.setAid(Integer.parseInt(request.getParameter("aid")));
		app.setName(request.getParameter("name"));
		app.setIcon(request.getParameter("icon"));
		app.setLeader(request.getParameter("leader"));
		app.setTeam(request.getParameter("team"));
		app.setStart_time(new Date());
		app.setSvn(request.getParameter("svn"));
		app.setImg_url(request.getParameter("img_url"));
		app.setDescription(request.getParameter("description"));
		app.setStatus(Integer.parseInt(request.getParameter("status")));
		AppDao dao=new AppDao();
		dao.update(app, Mode.UPDATE);
		
		
		
		return showApp(request, response);
		
	}
	

	@RequestMapping("/app/app_show.do")
	 public ModelAndView showApp(HttpServletRequest request,HttpServletResponse response) {
		Map map=new Hashtable();
		AppDao dao=new AppDao();
		App app=dao.getAppByAid(Integer.parseInt(request.getParameter("aid")));
		map.put("app", app);
		return new ModelAndView(getApp_show_view(),map);
	}
	
	@RequestMapping("/app/app_list.do")
    public String listApp(HttpServletRequest request,HttpServletResponse response) {

		AppDao dao=new AppDao();
		List<App> apps=dao.getAllApps();
		for(int i=0;i<apps.size();i++){
			App app=apps.get(i);
			String str=app.getDescription();
			if(str!=null){
				str=str.replaceAll("<.*>", "");
				str=str.replaceAll("<", "");
				str=str.replaceAll(">", "");
				if(str.length()>200)
					str=str.substring(0,201);
				}
			app.setDescription(str);
			apps.set(i, app);
		}
		request.setAttribute("apps", apps);
		
        return getApp_list_view();
    }
	
	@RequestMapping("/app/app_add.do")
    public ModelAndView addApp(HttpServletRequest request,HttpServletResponse response) {
		App app=new App();
		app.setName(request.getParameter("name"));
		app.setIcon(request.getParameter("icon"));
		app.setLeader(request.getParameter("leader"));
		app.setTeam(request.getParameter("team"));
		app.setStart_time(new Date());
		app.setSvn(request.getParameter("svn"));
		app.setImg_url(request.getParameter("img_url"));
		app.setStatus(Integer.parseInt(request.getParameter("status")));
		app.setDescription(request.getParameter("description"));

		HttpSession session=request.getSession();
		User user=(User)session.getAttribute("g305_user");
		app.setUsername(user.getUsername());
		
		AppDao dao=new AppDao();
		dao.update(app, Mode.INSERT);
		
		dao=new AppDao();
		List<App> apps=dao.getAllApps();
		for(int i=0;i<apps.size();i++){
			app=apps.get(i);
			if(app.getDescription()!=null&&app.getDescription().length()>150)
				app.setDescription(app.getDescription().substring(0,151));
		}
		Map map=new Hashtable();
		map.put("apps", apps);

		return new ModelAndView(getApp_list_view(),map);
    }
	
	@RequestMapping("/app/app_add_view.do")
    public String addAppView(HttpServletRequest request,HttpServletResponse response) {
        return getApp_add_view();
    }

	public String getApp_add_view() {
		return app_add_view;
	}

	public void setApp_add_view(String app_add_view) {
		this.app_add_view = app_add_view;
	}

	public String getApp_add_success_view() {
		return app_add_success_view;
	}

	public void setApp_add_success_view(String app_add_success_view) {
		this.app_add_success_view = app_add_success_view;
	}

	public String getApp_list_view() {
		return app_list_view;
	}

	public void setApp_list_view(String app_list_view) {
		this.app_list_view = app_list_view;
	}

	public String getApp_show_view() {
		return app_show_view;
	}

	public void setApp_show_view(String app_show_view) {
		this.app_show_view = app_show_view;
	}

	public String getApp_editor_view() {
		return app_editor_view;
	}

	public void setApp_editor_view(String app_editor_view) {
		this.app_editor_view = app_editor_view;
	}
	
	
}
